package pl.gda.pg.eti.mkdg.projekt;

import java.awt.*;
import java.awt.image.*;

public class Filter
{
    int w = 0;
    int h = 0;
    double[][] values;
    
    public Filter(int width, int height)
    {
        w = width;
        h = height;
        values = new double[h][w];
        
        for (int y=0; y<h; y++)
        {
            for (int x=0; x<w; x++)
            {
                values[y][x] = 0;
            }
        }
    }
    
    public Image filter(BufferedImage imgIn, boolean shouldFix)
    {
        BufferedImage imgOut = new BufferedImage(imgIn.getWidth(), imgIn.getHeight(), imgIn.getType());
        imgOut.setData(imgIn.getData());
        
        double[][] tmp = new double[imgIn.getWidth()][imgIn.getHeight()];

        for(int x = 0 ; x < imgIn.getWidth() ; x++)
        	for(int y = 0 ; y < imgIn.getHeight() ; y++)
        		tmp[x][y] = Double.NaN;
        
        int sx = w/2;
        int sy = h/2;

		double min = Double.MAX_VALUE;
		double max = Double.MIN_VALUE;
        
        for (int y = sy; y < imgIn.getHeight() - sy; y++) 
        {
            for (int x = sx; x < imgIn.getWidth() - sx; x++) 
            {
                double sumaR = 0;
                double sumaG = 0;
                double sumaB = 0;

                for (int q = -sy; q <= sy; q++) 
                {
                    for (int p = -sx; p <= sx; p++) 
                    {
                        int color = imgIn.getRGB(x + p, y + p);
                        double r = (double)((color & 0x00FF0000) >> 16);
                        double g = (double)((color & 0x0000FF00) >> 8);
                        double b = (double)(color & 0x000000FF);
                        
                        sumaR += r * values[q + sy][p + sx];
                        sumaG += g * values[q + sy][p + sx];
                        sumaB += b * values[q + sy][p + sx];
                    }
                }
               
                double brightness = sumaR * 0.299d + sumaG * 0.587d + sumaB * 0.114d;
                
                tmp[x][y] = brightness;
                
				if(brightness < min)
					min = brightness;
				if(brightness > max)
					max = brightness;
            }
        }

        for(int x = 0 ; x < imgIn.getWidth() ; x++)
        	for(int y = 0 ; y < imgIn.getHeight() ; y++) {
        		if(Double.isNaN(tmp[x][y]))
        			imgOut.setRGB(x, y, imgIn.getRGB(x, y));
        		else {
        			double fix = ((tmp[x][y] - min) / (max - min));
        			if(shouldFix)
        				fix = Math.abs(fix - 0.5d) * 2.0d;
            		int brightness = (int) (fix * 255.0d);
            		//System.out.println(brightness);
                    
                    int color = 0xFF000000; // ARGB
                    
                    color |= (brightness << 16);
                    color |= (brightness << 8);
                    color |= brightness;

                    imgOut.setRGB(x, y, color);
        		}
        	}
        
        return imgOut;
    }
}
